package com.cci.kangdao.task;

import com.cci.kangdao.common.RedisKeys;
import com.cci.kangdao.crmapi.CRMResponeEntity;
import com.cci.kangdao.crmapi.CRMServiceApi;
import com.cci.kangdao.crmapi.CRMServiceConst;
import com.cci.kangdao.escalation.dao.EscalationOrderTDao;
import com.cci.kangdao.escalation.model.EscalationOrderT;
import com.cci.kangdao.redis.RedisClientUtils;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.Date_Util;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Henry Liu
 * @date 2020/06/11
 */
@Component
public class EscalationOrderTask {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private RedisClientUtils redisClientUtils;

    @Resource
    private EscalationOrderTDao escalationOrderTDao;

    @Resource
    private CRMServiceApi crmServiceApi;

    /**
     * 定期处理redis中存储的升级工单催单记录
     */
    public void workOrderTask() {
        log.info("定时任务workOrderTask-开始");
        try {
            Map<String, String> map = redisClientUtils.getMap(RedisKeys.getRemindStatusForEscalation());
            if(null != map){
                map.forEach((key, value) -> {
                    EscalationOrderT escalationOrderT = escalationOrderTDao.selectByOrderNo(key);
                    if (null != escalationOrderT) {
                        if (1 != escalationOrderT.getStatus()) {
                            redisClientUtils.hdel(RedisKeys.getRemindStatusForEscalation(), key);
                        } else {
                            long subTime = Date_Util.getSubTime(escalationOrderT.getCreateTime(), LocalTimeUtil.getRealCurrentTime(), 2);
                            if (subTime >= 60) {
                                redisClientUtils.hdel(RedisKeys.getRemindStatusForEscalation(), key);
                            }
                        }
                    } else {
                        redisClientUtils.hdel(RedisKeys.getRemindStatusForEscalation(), key);
                    }
                });
            }
        } catch (Exception e) {
            log.error("定时任务workOrderTask发生异常，异常信息" + e.getMessage(), e);
        }
        log.info("定时任务workOrderTask-结束");

    }

    /**
     * 同步主动升级详情信息至CRM： 每小时执行一次
     */
    public void syncESOrderDetail() {
        List<String> exceptionSyncKeyList = redisClientUtils.keys(CRMServiceConst.ESCALATION_SYNC_EXCEPTION + "*");
        if (CollectionUtils.isNotEmpty(exceptionSyncKeyList)) {
            for (String exceptionSyncKey : exceptionSyncKeyList) {
                retryExceptionSyncByKey(exceptionSyncKey);
            }
        }
    }

    // 根据ExceptionSyncKey进行Retry
    private void retryExceptionSyncByKey(String exceptionSyncKey) {
        List<String> exceptionList = redisClientUtils.getObjectList(exceptionSyncKey);
        if (CollectionUtils.isEmpty(exceptionList)) {
            return;
        } else {
            for (String escalationId : exceptionList) {
                CRMResponeEntity responseEntity = null;
                switch (exceptionSyncKey) {
                    case CRMServiceConst.ESCALATION_SYNC_EXCEPTION_DETAIL:
                        responseEntity = syncESOrderDetailToCRM(escalationId);
                        break;
                    default:
                        log.error("主动升级工单异常重试，未知类型" + exceptionSyncKey);
                        return;
                }
                if (responseEntity.getStatus() == 0) {
                    redisClientUtils.listObjectRemove(exceptionSyncKey, escalationId);
                } else {
                    return;
                }
            }
        }
    }

    // 处理EscalationDetail信息
    private CRMResponeEntity syncESOrderDetailToCRM(String escalationId) {
        EscalationOrderT escalationOrderT = escalationOrderTDao.selectByPrimaryKey(Long.valueOf(escalationId));
        CRMResponeEntity crmResponeEntity = crmServiceApi.syncEscalationOrderDetail(escalationOrderT, 1);
        return crmResponeEntity;
    }
}
